/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.unipampa.utilitarios;

import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

/**
 *
 * @author gabrielbmoro
 */
public class ExecutaSom {

    // Abrir um fluxo de entrada de áudio.
    private URL url;
    private AudioInputStream audioIn;
    private Clip clip;
    private static int LOOP_ALARME=1000;
    
    public void pararAlarme(){
        if(this.clip!=null){
            clip.stop();
        }
    }
    public void executarAlarme(){
        url = this.getClass().getResource("/br/unipampa/sounds/alarme.wav");

        try {
            audioIn = AudioSystem.getAudioInputStream(url);
            //Obter um recurso clipe de som.
            clip = AudioSystem.getClip();
            //abre o arquivo
            clip.open(audioIn);

            audioIn = AudioSystem.getAudioInputStream(getUrl());
            //Obter um recurso clipe de som.
            clip = AudioSystem.getClip();
            clip.loop(LOOP_ALARME);
            //abre o arquivo
            clip.open(audioIn);
            //executa o som
            clip.start();
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
    }

    public void executar(String CaminhoSom) {
        url = this.getClass().getResource(CaminhoSom);

        try {
            audioIn = AudioSystem.getAudioInputStream(getUrl());
            //Obter um recurso clipe de som.
            clip = AudioSystem.getClip();
            //abre o arquivo
            clip.open(audioIn);

            audioIn = AudioSystem.getAudioInputStream(getUrl());
            //Obter um recurso clipe de som.
            clip = AudioSystem.getClip();
            //abre o arquivo
            clip.open(audioIn);
            //executa o som
            clip.start();
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
    }

    public URL getUrl() {
        return url;
    }

    public void setUrl(URL url) {
        this.url = url;
    }
}
